package day10;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

//Demo01类开始
public class Demo01 {
    public static void main(String[] args) {
        JFrame f = new JFrame("捕鱼");
        f.setSize(800,510);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FishPanel fp = new FishPanel();
        f.add(fp);
        f.setVisible(true);
        fp.action();
    }
}//Demo01类结束
//FishPanel开始
class FishPanel extends JPanel{
    BufferedImage bg;
    Fish fish13;
    Fish fish10;
    Fish[] fishes;
    public FishPanel(){
        try {
            bg = ImageIO.read(new File("images/bg.jpg"));
            fish13 = new Fish("fish13");
            fish10 = new Fish("fish14");
            fishes = new Fish[9+9+9+2];
            for(int i=0;i<9;i++){
                fishes[i] = new Fish("fish0"+(i+1));
                fishes[i+9] = new Fish("fish0"+(i+1));
                fishes[i+9+9] = new Fish("fish0"+(i+1));
            }
            fishes[fishes.length-2] = new Fish("fish13");
            fishes[fishes.length-1] = new Fish("fish14");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void action(){
        while(true){
            fish13.move();
            fish10.move();
            for(int i=0;i<fishes.length;i++){
                fishes[i].move();
            }
            try {
                Thread.sleep(1000/24);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            repaint();
        }
    }
    @Override
    public void paint(Graphics g) {
        g.drawImage(bg,0,0,null);
        g.drawImage(fish13.img,fish13.x,fish13.y,null);
        g.drawImage(fish10.img,fish10.x,fish10.y,null);
        for(int i=0;i<fishes.length;i++){
            g.drawImage(fishes[i].img,fishes[i].x,fishes[i].y,null);
        }
    }

}
//FishPanel结束
//Fish类开始
class Fish{
    //鱼的当前图片
    BufferedImage img;
    //用来存储每条鱼的10张图片
    BufferedImage[] imgs;
    //x坐标
    int x;
    //y坐标
    int y;
    //鱼图片的宽
    int width;
    //鱼图片的高
    int height;
    //鱼游动的速度
    int step;
    int index;

    //构造方法，用来给属性赋值
    public Fish(String name){
        imgs = new BufferedImage[10];//根据每条鱼的图片数量决定
        //images/fish01_00.png
        try {
            for(int i=0;i<imgs.length;i++){
                String file = "images/"+name+"_0"+i+".png";
                System.out.println(file);
                imgs[i] = ImageIO.read(new File(file));
            }
            img = imgs[0];//将数组中的第一个张图片给了当前图片
            Random r = new Random();
            x = r.nextInt(700);
            y = r.nextInt(450);
            width = img.getWidth();
            height = img.getHeight();
            step = r.nextInt(8)+1;
            index = 0;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //鱼儿游动的方法
    public void move(){
        x -= step;
        img = imgs[index++ % imgs.length];
        /**
         * 鱼儿游出界后，要从右边再游进来
         * 但是y坐标可以不一样，速度可以不一样
         */
        if(x<-width){
            x = 800;
            Random r = new Random();
            y = r.nextInt(450)+1;
            step = r.nextInt(8)+1;
        }
    }


    @Override
    public String toString() {
        return "Fish{" +
                "img=" + img +
                ", x=" + x +
                ", y=" + y +
                ", width=" + width +
                ", height=" + height +
                ", step=" + step +
                '}';
    }
}
//Fish类结束











